<?php

if (!defined('BASEPATH')) exit('No direct script access allowed');

/**
 * 公共辅助函数
 * 扩展方法 http://codeigniter.org.cn/user_guide/general/helpers.html
 *
 * @Thanks https://github.com/phpwind/windframework
 */


/**
 * logged_in
 *
 * @return bool
 * @author Mathew
 **/
if (!function_exists('logged_in'))
{
	function logged_in()
	{
		$CI = &get_instance();
		return (bool)$CI->session->userdata('uid');
	}
}

/**
 * 通过淘宝API接口查询客户端地址
 * 淘宝API:http://ip.taobao.com/service/getIpInfo.php?ip=ip地址
 * 接口说明:http://ip.taobao.com/instructions.php
 *
 * 返回信息
 * object(stdClass)
 * public 'code' => int 0
 * public 'data' =>
 * object(stdClass)
 * public 'country' => string '中国'
 * public 'country_id' => string '86'
 * public 'area' => string '华东'
 * public 'area_id' => string '300000'
 * public 'region' => string '江苏省'
 * public 'region_id' => string '320000'
 * public 'city' => string '无锡市'
 * public 'city_id' => string '320200'
 * public 'county' => string ''
 * public 'county_id' => string '-1'
 * public 'isp' => string '电信'
 * public 'isp_id' => string '100017'
 * public 'ip' => string '58.215.75.40'
 */
if (!function_exists('get_ip_data'))
{
	function get_ip_data($ip)
	{
		$ip = file_get_contents('http://ip.taobao.com/service/getIpInfo.php?ip=' . $ip);
		return json_decode($ip);
	}
}

/**
 * 显示消息
 *
 * @param $message string 消息
 * @param $state string  'fail' or 'success' 状态
 * @param $referer string 跳转地址
 * @param $refresh bool 是否刷新
 * @update 2012-10-22 15:42
 *
 * 出现问题参考：http://codeigniter.org.cn/forums/thread-11276-1-1.html
 * 例如：开启了gZip压缩（$config['compress_output'] = TRUE;）+ajax数据返回
 *       不能在任何控制器里使用echo，要使用$this->output->set_output($html) 且要放在最后
 */
if (!function_exists('show_msg'))
{
	function show_msg($message = '', $state = 'fail', $referer = '', $refresh = FALSE)
	{
		$CI = &get_instance();

		$data['__error'] = '';
		$data['referer'] = $referer;
		$data['refresh'] = $refresh;
		$data['state'] = $state;
		$data['message'] = array($message);

		if ($CI->input->is_ajax_request())
		{
			$CI->output->enable_profiler(false); //禁用调试信息在底部输出
			$CI->config->set_item('compress_output', FALSE); //关闭gzip压缩
			$json = json_encode($data);
			echo $json;
			//$CI->output->set_content_type('application/json')->set_output($json);
			exit;
		}

		echo $CI->load->view('common_showmsg', $data, TRUE);
		exit;
	}
}

/**
 * 显示后台消息
 *
 * @param $message string 消息
 * @param $state string  'fail' or 'success' 状态
 * @param $referer string 跳转地址
 * @param $refresh bool 是否刷新
 * @update 2012-11-21 20:33
 *
 * 出现问题参考：http://codeigniter.org.cn/forums/thread-11276-1-1.html
 * 例如：开启了gZip压缩（$config['compress_output'] = TRUE;）+ajax数据返回
 *       不能在任何控制器里使用echo，要使用$this->output->set_output($html) 且要放在最后
 */
if (!function_exists('show_admin_msg'))
{
	function show_admin_msg($message = '', $state = 'fail', $referer = '', $refresh = FALSE)
	{
		$CI = &get_instance();

		$data['__error'] = '';
		$data['referer'] = $referer;
		$data['refresh'] = $refresh;
		$data['state'] = $state;
		$data['message'] = array($message);

		if ($CI->input->is_ajax_request())
		{
			$CI->output->enable_profiler(false); //禁用调试信息在底部输出
			$CI->config->set_item('compress_output', FALSE); //关闭gzip压缩
			$json = json_encode($data);
			echo $json;
			//$CI->output->set_content_type('application/json')->set_output($json);
			exit;
		}

		echo $CI->load->view('admin/common_showmsg', $data, TRUE);
		exit;
	}
}


/* End of file common_helper.php */
/* Location: ./application/helpers/common_helper.php */
